import 'package:amway_superapp_creator_plugin/config/colors_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommTipDialog extends StatefulWidget {
  final bool outsideDismiss; //点击弹窗外部，关闭弹窗，默认为true true：可以关闭 false：不可以关闭
  final Function confirmCallback; //点击确定按钮回调
  final Function dismissCallback;
  final String content;
  final String confirmText;
  final String closeText;
  final bool isEditInfo;

  const CommTipDialog(
      {Key key,
      this.content,
      this.outsideDismiss = true,
      this.confirmText,
      this.closeText,
      this.isEditInfo = false,
      this.confirmCallback,
      this.dismissCallback})
      : super(key: key); //弹窗关闭回调

  @override
  _CommTipDialogState createState() => _CommTipDialogState();
}

class _CommTipDialogState extends State<CommTipDialog> {
  _confirmDialog() {
    if (widget.confirmCallback != null) {
      widget.confirmCallback();
    }
  }

  _dismissDialog() {
    if (widget.dismissCallback != null) {
      widget.dismissCallback();
    }
  }

  _dismiss() {
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return WillPopScope(
        child: GestureDetector(
          onTap: () {},
          child: Material(
            type: MaterialType.transparency,
            child: Center(
              child: Container(
                padding: EdgeInsets.only(left: 24, right: 24, bottom: 24),
                width: size.width * 0.85,
                child: Container(
                  child: Column(
                    mainAxisSize: MainAxisSize.min, //自适应高度
                    children: [
                      Container(
                        padding: EdgeInsets.only(left: 24, right: 24, top: 37),
                        child: Center(
                          child: Text(
                            widget.content,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: ColorsConfig.c_2c2c2c,
                                fontSize: ScreenUtil().setSp(16)),
                          ),
                        ),
                      ),
                      Container(
                        alignment: Alignment.bottomCenter,
                        margin: EdgeInsets.only(top: 38),
                        child: Row(
                          children: [
                            Expanded(
                              flex: widget.confirmText.length > 4 ? 5 : 1,
                              child: Container(
                                height: ScreenUtil().setHeight(50),
                                child: RaisedButton(
                                  padding: EdgeInsets.symmetric(vertical: 8),
                                  onPressed: () {
                                    if (widget.isEditInfo != null &&
                                        widget.isEditInfo == false) {
                                      _dismiss();
                                    }
                                    _dismissDialog();
                                  },
                                  child: Text(
                                    widget.closeText != null
                                        ? widget.closeText
                                        : '取消',
                                    style: TextStyle(
                                        color: ColorsConfig.c_396E75,
                                        fontSize: ScreenUtil().setSp(16)),
                                  ),

                                  textTheme: ButtonTextTheme.normal,
                                  color: Colors.white,
                                  disabledColor: Colors.grey,
                                  highlightColor: Colors.grey,
                                  // 按下的背景色
                                  colorBrightness: Brightness.light,

                                  elevation: 0,
                                  shape: RoundedRectangleBorder(
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(20)),
                                      side: BorderSide(
                                          color: ColorsConfig.c_396E75,
                                          style: BorderStyle.solid,
                                          width: 1)),
                                ),
                              ),
                            ),
                            SizedBox(width: ScreenUtil().setWidth(20)),
                            Expanded(
                              flex: widget.confirmText.length > 4 ? 6 : 1,
                              child: Container(
                                height: ScreenUtil().setHeight(50),
                                child: RaisedButton(
                                  padding: EdgeInsets.symmetric(vertical: 8),
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(20),
                                  ),
                                  textTheme: ButtonTextTheme.normal,
                                  textColor: Colors.white,
                                  disabledTextColor: ColorsConfig.c_396E75,
                                  color: ColorsConfig.c_396E75,
                                  disabledColor: Colors.grey,
                                  highlightColor: Colors.grey,
                                  // 按下的背景色
                                  splashColor: ColorsConfig.c_396E75,
                                  // 水波纹颜色
                                  colorBrightness: Brightness.light,

                                  onPressed: () {
                                    if (widget.isEditInfo != null &&
                                        widget.isEditInfo == false) {
                                      _dismiss();
                                    }
                                    _confirmDialog();
                                  },
                                  child: Text(
                                    widget.confirmText != null
                                        ? widget.confirmText
                                        : '确定',
                                    maxLines: 1,
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: ScreenUtil().setSp(16)),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                decoration: BoxDecoration(
                    color: Color(0xFFFFFFFF),
                    borderRadius: BorderRadius.circular(10.0)),
              ),
            ),
          ),
        ),
        onWillPop: () async {
          return widget.outsideDismiss ? _dismiss() : null;
        });
  }
}
